﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using chihuopig.Web.Framework;
using Microsoft.AspNetCore.Http;
using chihuopig.IService;
using chihuopig.Web.verfiy;
using chihuopig.Service;

namespace chihuopig.Web.Admin.Controllers
{
    public class adminruleController : AdminWattantyController
    {
        IHttpContextAccessor _iHttpContextAccessor;
        IAuthorization _iAuthorization;
        IAdminMenuService _iAdminMenuService;
        public adminruleController
           (
               IHttpContextAccessor iHttpContextAccessor,
               IAuthorization iAuthorization,
               IAdminMenuService AdminMenuService

           ) :base(iHttpContextAccessor) 
        {

            _iHttpContextAccessor = iHttpContextAccessor;
            _iAuthorization = iAuthorization;
            _iAdminMenuService = AdminMenuService;
        }

        public async Task<IActionResult> adminruleAdd()
        {
            ViewBag.GetAdminMenu = await _iAdminMenuService.GetAdminMenu();
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> adminruleAdd(jurisdictionVerfiy model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    DBService.Sqlclient.Ado.BeginTran();
                    await _iAuthorization.Insertable(new Model.jurisdiction() { Page = model.UserName, Prerogative = model.AuthorCode, Name = model.AuthorName, MenuId = model.MenuId, IsDelete = false, Addtime = DateTime.Now });
                    DBService.Sqlclient.Ado.CommitTran();
                    return Json(new JsonMsg() { succeed = true, msg = "添加成功" });
                }
                catch (Exception ex)
                {
                    DBService.Sqlclient.Ado.RollbackTran();
                    return Json(new JsonMsg() { succeed = false, msg = "添加失败" });
                }

            }

            return Json(ModelStateVefify.Vefifymsg(ModelState));
        }

        public  async Task<IActionResult> adminruleEdit(long id)
        {

            var model=await _iAuthorization.GetjurisdictionById(id);
            ViewBag.GetAdminMenu= await _iAdminMenuService.GetAdminMenu();
            ViewBag.adminruleId = id;
            return View(model);
        }

        [HttpPost]
        public async Task<IActionResult> adminruleEdit(jurisdictionVerfiy model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    DBService.Sqlclient.Ado.BeginTran();
                    await _iAuthorization.Updateable(new Model.jurisdiction() { Page = model.UserName, Id = model.adminruleId, Prerogative = model.AuthorCode, Name = model.AuthorName, MenuId = model.MenuId });
                    DBService.Sqlclient.Ado.CommitTran();
                    return Json(new JsonMsg() { succeed = true, msg = "修改成功" });
                }
                catch(Exception ex)
                {
                    DBService.Sqlclient.Ado.RollbackTran();
                    return Json(new JsonMsg() { succeed = false, msg = "修改失败" });
                }

             
            }

           return Json(ModelStateVefify.Vefifymsg(ModelState));
        }

        [HttpPost]
        public async Task<IActionResult> delAll(long[] id)
        {
            var resust = await _iAuthorization.delAll(id);
            if (resust > 0)
            {
                return Json(new { succeed = true, msg = "删除成功" });
            }
            return Json(new { succeed = false, msg = "删除失败" });
        }
        public async Task<IActionResult> List(int Pageindex = 1, int PageSize =10)
        {
            var list = await _iAuthorization.GetPage(Pageindex, PageSize);
            var total = await  _iAuthorization.GetPageTotal(PageSize);
            ViewBag.Pageindex = Pageindex;
            ViewBag.Start = Pageindex + 5 > total ? 1 : Pageindex + 5;
            ViewBag.GetPageTotal = total + 5 > total ? total : total + 5;
            return View(list);
        }
    }
}